<?php

/**
 * This file is part of the Framework03 PHP framework.
 * @author Walter Tamboer
 * @package Framework03
 */

class FW_Uri_Mailto extends FW_Uri_Abstract
{
    /**
     * @var string The URN of this URI.
     */
    private $urn;

    /**
     * @var string The "to address" of this URI.
     */
    private $to = '';

    /**
     * @var string The host of this URI.
     */
    private $host = '';

    /**
     * @var array The headers of this URI.
     */
    private $headers = array();

    /**
     * Initializes a new instance of this class.
     * @param string $urn The unified resource name.
     */
    public function __construct($urn)
    {
        $this->urn = $urn;

        $pattern = '/^([a-zA-Z0-9_]*)(@([^?]*))(\?(.*))?$/';
        $status = @preg_match($pattern, $urn, $matches);

        $this->to = isset($matches[1]) ? $matches[1] : $this->to;
        $this->host = isset($matches[3]) ? $matches[3] : $this->host;
        $this->headers = isset($matches[5]) ? explode('&', $matches[5]) : $this->headers;
    }

    /**
     * Gets the scheme of this URI.
     * @return string
     */
    public function getScheme()
    {
        return 'mailto';
    }
    
    public function getTo()
    {
        return $this->to;
    }

    /**
     * Gets the host of this URI.
     * @return string
     */
    public function getHost()
    {
        return $this->host;
    }

    /**
     * Gets the headers of this URI.
     * @param bool $asString Whether or not to return a string or an array.
     * @return array|string
     */
    public function getHeaders($asString = true)
    {
        if ($asString)
            return implode('&', $this->headers);
        else
            return $this->headers;
    }

    /**
     * Returns the Urn of this URI.
     * @return string
     */
    public function getUrn()
    {
        return $this->urn;
    }

    /**
     * Converts this URI to a string.
     * @return string.
     */
    public function __toString()
    {
        return $this->getScheme() . ':' . $this->getUrn();
    }
}
